package com.example.da20;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.example.Easy2LearnApplication;
import org.example.domain.Manual;
import org.example.domain.Question;
import org.example.excel.DataDAO;
import org.example.excel.ExcelListener;
import org.example.mapper.ManualMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest(classes = {Easy2LearnApplication.class, TestConfig.class})
public class GetQuestion {

    @Resource
    private ManualMapper manualMapper;

    /**
     * 从manul表中提取出题目
     */
    @Test
    public void get20Question() {
        List<Manual> list = manualMapper.selectList(new LambdaQueryWrapper<Manual>().ne(Manual::getQuestions,""));
        List<Question> questionList = new ArrayList<>();
        for (Manual manual : list) {
            String questions = manual.getQuestions();
            String[] split = questions.split("\\n");
            int index = 0;
            Question question = new Question();
//            question.setChapter(manual.getIndex());
            StringBuilder stringBuilder = new StringBuilder();
            for (String s : split) {
                if (!"".equals(s)) {
                    index++;
                    switch (index)
                        {
                            case 1:
                                System.out.println("题目：" + s);
                                if (s.contains("题目：")) {
                                    s = s.replace("题目：","");
                                    System.out.println(s);
                                    question.setQuestion(s);
                                }else {
                                    question.setQuestion(s);
                                }
                                break;
                            case 2:
                                System.out.println("选项1：" + s);
                                question.setItem1(s);
                            break;
                            case 3:
                                System.out.println("选项2：" + s);
                                question.setItem2(s);
                                break;
                            case 4:
                                System.out.println("选项3：" + s);
                                question.setItem3(s);
                                break;
                            case 5:
                                System.out.println("选项4：" + s);
                                question.setItem4(s);
                                break;
                            case 6:
                                System.out.println("答案：" + s);
                                if (s.contains("A")) {
                                    s="A";
                                } else if (s.contains("B")) {
                                    s="B";
                                } else if (s.contains("C")) {
                                    s="C";
                                } else if (s.contains("D")) {
                                    s="D";
                                } else {
                                    System.out.println("error");
                                }
                                question.setAnswer(s);
                                break;
                            case 7:
                                System.out.println("解析：" + s);
                                stringBuilder.append(s);
                                break;
                            default:
                                stringBuilder.append(s);
                                System.out.println("error123"+s);
                        }
                }
            }
            question.setRemark(stringBuilder.toString());
            questionList.add(question);
        }
        int index = 2;
        for (Question question : questionList) {
            if (!question.getItem1().startsWith("A")||!question.getItem2().startsWith("B")||!question.getItem3().startsWith("C")||!question.getItem4().startsWith("D")) {
                System.out.println("error"+index);
            }
            index++;
        }
//        EasyExcel.write("C:\\Users\\SIM\\Desktop\\Da20.xlsx", Question.class).sheet().doWrite(questionList);
    }

    public static void main(String[] args) {
        String url = "C:\\Users\\SIM\\Desktop\\Da20.xlsx";
        DataDAO<Question> dao = new DataDAO<>();
        EasyExcel.read(url, Question.class, new ExcelListener<>(dao)).sheet().doRead();
        List<Question> newQuestions = new ArrayList<>();
        for (Question q : dao.getList()) {
//            String chapter = q.getChapter();
            String question1 = q.getQuestion();
            String item1 = q.getItem1();
            String item2 = q.getItem2();
            String item3 = q.getItem3();
            String item4 = q.getItem4();
            String answer = q.getAnswer();
            String remark = q.getRemark();
            /**
             * 打印上面get的数据，获取用户输入，如果用户什么也不输入表示同意，用户输入任意一个字符表示不同意，如果用户同意，就把该question加入到newQuestions中
             */
            if (!((question1.contains("DA20-C1飞机的维护手册（AMM）中")
                    &&question1.contains("哪一部分"))
                    &&!question1.contains("故意留空"))
                    &&!question1.contains("内容为空")
                    &&!question1.contains("被留空")
                    &&!question1.contains("Intentionally Left Blank")
                    &&!question1.contains("的内容是什么")
            ) {
                newQuestions.add(q);
            }
//            System.out.println("章节：" + chapter);
//            System.out.println("题目：" + question1);
//            System.out.println("选项1：" + item1);
//            System.out.println("选项2：" + item2);
//            System.out.println("选项3：" + item3);
//            System.out.println("选项4：" + item4);
//            System.out.println("答案：" + answer);
//            System.out.println("解析：" + remark);
//            System.out.println("是否同意：1表示同意，其他输入表示不同意");
//            Scanner scanner = new Scanner(System.in);
//            String next = scanner.next();
//            if ("1".equals(next)) {
//                newQuestions.add(q);
//            }
        }
        EasyExcel.write("DA20R1.xlsx", Question.class).sheet().doWrite(newQuestions);

    }
}
